package com.rskj.cashbook.controller;

import com.rskj.cashbook.response.ResponseEntity;
import com.rskj.cashbook.service.IDictTypeService;
import com.rskj.cashbook.vo.DictType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典类型控制器
 *
 * @author fudaopin
 */
@RestController
@RequestMapping("/api/dict-type")
@CrossOrigin(origins = "*")
public class DictTypeController {

    @Autowired
    private IDictTypeService dictTypeService;

    /**
     * 获取所有字典类型
     */
    @GetMapping("/list")
    public ResponseEntity<List<DictType>> getAll() {
        try {
            List<DictType> list = dictTypeService.getAll();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 获取所有启用的字典类型
     */
    @GetMapping("/list-enabled")
    public ResponseEntity<List<DictType>> getAllEnabled() {
        try {
            List<DictType> list = dictTypeService.getAllEnabled();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据ID获取字典类型
     */
    @GetMapping("/{id}")
    public ResponseEntity<DictType> getById(@PathVariable String id) {
        try {
            DictType dictType = dictTypeService.getById(id);
            if (dictType != null) {
                return ResponseEntity.ok(dictType);
            } else {
                throw new RuntimeException("查询不到字典");
            }
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    /**
     * 根据编码获取字典类型
     */
    @GetMapping("/code/{code}")
    public ResponseEntity<DictType> getByCode(@PathVariable String code) {
        try {
            DictType dictType = dictTypeService.getByCode(code);
            if (dictType != null) {
                return ResponseEntity.ok(dictType);
            } else {
                throw new RuntimeException("字典类型不存在");
            }
        } catch (Exception e) {
            throw new RuntimeException("获取字典类型失败：" + e.getMessage());
        }
    }

    /**
     * 保存字典类型
     */
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody DictType dictType) {
        try {
            // 检查编码是否已存在
            if (dictTypeService.isCodeExists(dictType.getCode())) {
                throw new RuntimeException("字典类型编码已存在");
            }

            int result = dictTypeService.save(dictType);
            if (result > 0) {
                return ResponseEntity.ok("保存成功");
            } else {
                throw new RuntimeException("保存失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("保存字典类型失败：" + e.getMessage());
        }
    }

    /**
     * 更新字典类型
     */
    @PutMapping("/update")
    public ResponseEntity<String> update(@RequestBody DictType dictType) {
        try {
            int result = dictTypeService.update(dictType);
            if (result > 0) {
                return ResponseEntity.ok("更新成功");
            } else {
                throw new RuntimeException("更新失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("更新字典类型失败：" + e.getMessage());
        }
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> delete(@PathVariable String id) {
        try {
            int result = dictTypeService.delete(id);
            if (result > 0) {
                return ResponseEntity.ok("删除成功");
            } else {
                throw new RuntimeException("删除失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("删除字典类型失败：" + e.getMessage());
        }
    }

    /**
     * 根据编码删除字典类型
     */
    @DeleteMapping("/code/{code}")
    public ResponseEntity<String> deleteByCode(@PathVariable String code) {
        try {
            int result = dictTypeService.deleteByCode(code);
            if (result > 0) {
                return ResponseEntity.ok("删除成功");
            } else {
                throw new RuntimeException("删除失败");
            }
        } catch (Exception e) {
            throw new RuntimeException("删除字典类型失败：" + e.getMessage());
        }
    }
}
